import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


public class ToolKitBar extends JFrame implements ActionListener{
	
	private int screenHeight = 0;
	private int screenWidth = 0;
	private final int HEIGHT_OFFSET = 411;
	private final int WIDTH_OFFSET = 5;
	private  TinCDCanvas canvas;
	
	private Button alkaneButton = new Button("C-C");
	private Button benzeneButton = new Button("Benzene");
	private Button alkaneChainButton = new Button("[C-C]n");
	private Button cycloHexaneButton = new Button("Cylcohexane");
	private Button textButton = new Button("Insert Text");
	private Button selectButton = new Button("Selection Tool");
	private Button deleteButton = new Button("Delete");
	private Button atomchooserButton = new Button("Atom Chooser");
	private Button cycloPentaneButton = new Button("Cyclopentane");
	private Button doublebondButton = new Button("Double Bond");
	private Button triplebondButton = new Button("Triple Bond");
	private Button wedgeButton = new Button("Wedge Bond");
	private Button dashButton = new Button("Dash Bond");

	static boolean chooserInstance = false;

	
	public ToolKitBar(String toolKitName,TinCDCanvas twoDCanvas)
	{
		super(toolKitName);
		canvas = twoDCanvas;
		
		getScreenSize();
		
		this.setVisible(true);
		this.setSize(550,96);
		this.setLocation(screenWidth, screenHeight);
		this.setResizable(false);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE); 
		/*NOTE!!#@@!*
		 * Make sure to have an option to bring this back if you close it!
		 */
		 setLayout(new GridLayout(3,5));
         add(alkaneButton);
         //alkaneButton.setSize(30, 30);
         add(benzeneButton);
         add(alkaneChainButton);
         add(cycloHexaneButton);
         add(textButton);
         add(selectButton);
         add(deleteButton);
         add(atomchooserButton);
         add(cycloPentaneButton);
         add(doublebondButton);
         add(triplebondButton);
         add(wedgeButton);
         add(dashButton);
         

         alkaneButton.addActionListener(this);
         alkaneChainButton.addActionListener(this);
         cycloHexaneButton.addActionListener(this);
         benzeneButton.addActionListener(this);
		 textButton.addActionListener(this);
		 selectButton.addActionListener(this);
		 deleteButton.addActionListener(this);
		 atomchooserButton.addActionListener(this);
		 cycloPentaneButton.addActionListener(this);
		 doublebondButton.addActionListener(this);
		 triplebondButton.addActionListener(this);
		 wedgeButton.addActionListener(this);
		 dashButton.addActionListener(this);

	}

	public void getScreenSize()
	{
		Toolkit javaToolKit = Toolkit.getDefaultToolkit();
		Dimension screenSize = javaToolKit.getScreenSize();
		screenHeight = screenSize.height/4+HEIGHT_OFFSET;
		screenWidth = screenSize.width/4+WIDTH_OFFSET;
		
	}
	
	public void unhideToolKit()
	{
		this.show();
}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		if (e.getSource() == alkaneButton)
		{
			canvas.drawLine = true;
			canvas.drawTextBox = false;
			canvas.drawText = false;
			canvas.drawChain = false;
			canvas.drawBenzene = false;
			canvas.drawCyclohexane = false;
			canvas.drawWedge = false;
			canvas.drawDash = false;
			canvas.drawDouble = false;
			canvas.drawTriple = false;
			
	
		}
		if(e.getSource() == alkaneChainButton)
		{
			canvas.drawLine = false;
			canvas.drawTextBox = false;
			canvas.drawText = false;
			canvas.drawChain = true;
			canvas.drawBenzene = false;
			canvas.drawCyclohexane = false;
			canvas.drawWedge = false;
			canvas.drawDash = false;
			canvas.drawDouble = false;
			canvas.drawTriple = false;
		}
		if(e.getSource() == benzeneButton)
		{
			canvas.drawLine = false;
			canvas.drawTextBox = false;
			canvas.drawText = false;
			canvas.drawChain = false;
			canvas.drawBenzene = true;
			canvas.drawCyclohexane = false;
			canvas.drawWedge = false;
			canvas.drawDash = false;
			canvas.drawDouble = false;
			canvas.drawTriple = false;
		}
		if(e.getSource() == cycloHexaneButton)
		{
			canvas.drawLine = false;
			canvas.drawTextBox = false;
			canvas.drawText = false;
			canvas.drawChain = false;
			canvas.drawBenzene = false;
			canvas.drawCyclohexane = true;
			canvas.drawWedge = false;
			canvas.drawDash = false;
			canvas.drawDouble = false;
			canvas.drawTriple = false;
		}
		if(e.getSource() == textButton)
		{
			canvas.drawLine = false;
			canvas.drawTextBox = true;
			canvas.drawText = true;
			canvas.drawChain = false;
			canvas.drawBenzene = false;
			canvas.drawCyclohexane = false;
			canvas.drawWedge = false;
			canvas.drawDash = false;
			canvas.drawDouble = false;
			canvas.drawTriple = false;
		}
		
		if(e.getSource() == selectButton)
		{
			canvas.drawLine = false;
			canvas.drawTextBox = false;
			canvas.drawText = false;
			canvas.drawChain = false;
			canvas.drawBenzene = false;
			canvas.drawCyclohexane = false;
			canvas.drawWedge = false;
			canvas.drawDash = false;
			canvas.drawDouble = false;
			canvas.drawTriple = false;
		}
		if(e.getSource() == deleteButton)
		{
			canvas.drawLine = false;
			canvas.drawTextBox = false;
			canvas.drawText = false;
			canvas.drawChain = false;
			canvas.drawBenzene = false;
			canvas.drawCyclohexane = false;
			canvas.drawWedge = false;
			canvas.drawDash = false;
			canvas.drawDouble = false;
			canvas.drawTriple = false;
		}
		if(e.getSource() == atomchooserButton)
		{
			//System.out.println("Atom Chooser HIT!");
			if(chooserInstance == false)
			{	
				AtomChooser atomChooser = new AtomChooser();
				chooserInstance = true;
			}
			
			//this.add(atomChooser);
			//atomChooser.setVisible(true);
			
		}
		if(e.getSource() == cycloPentaneButton)
		{
			canvas.drawLine = false;
			canvas.drawTextBox = false;
			canvas.drawText = false;
			canvas.drawChain = false;
			canvas.drawBenzene = false;
			canvas.drawCyclohexane = false;
			canvas.drawWedge = false;
			canvas.drawDash = false;
			canvas.drawDouble = false;
			canvas.drawTriple = false;
		}
		if(e.getSource() == doublebondButton)
		{
			canvas.drawLine = false;
			canvas.drawTextBox = false;
			canvas.drawText = false;
			canvas.drawChain = false;
			canvas.drawBenzene = false;
			canvas.drawCyclohexane = false;
			canvas.drawWedge = false;
			canvas.drawDash = false;
			canvas.drawDouble = true;
			canvas.drawTriple = false;
		}
		if(e.getSource() == triplebondButton)
		{
			canvas.drawLine = false;
			canvas.drawTextBox = false;
			canvas.drawText = false;
			canvas.drawChain = false;
			canvas.drawBenzene = false;
			canvas.drawCyclohexane = false;
			canvas.drawWedge = false;
			canvas.drawDash = false;
			canvas.drawDouble = false;
			canvas.drawTriple = true;
		}
		
		if(e.getSource() == wedgeButton)
		{
			canvas.drawLine = false;
			canvas.drawTextBox = false;
			canvas.drawText = false;
			canvas.drawChain = false;
			canvas.drawBenzene = false;
			canvas.drawCyclohexane = false;
			canvas.drawWedge = true;
			canvas.drawDash = false;
			canvas.drawDouble = false;
			canvas.drawTriple = false;
		}
		if(e.getSource() == dashButton)
		{
			canvas.drawLine = false;
			canvas.drawTextBox = false;
			canvas.drawText = false;
			canvas.drawChain = false;
			canvas.drawBenzene = false;
			canvas.drawCyclohexane = false;
			canvas.drawWedge = false;
			canvas.drawDash = true;
			canvas.drawDouble = false;
			canvas.drawTriple = false;
		}
			
		}
	
class AtomChooser extends JFrame implements MouseListener, WindowListener
{


	JLabel hydrogen = new JLabel();
	JLabel carbon = new JLabel();
	JLabel nitrogen = new JLabel();
	JLabel oxygen = new JLabel();
	JLabel fluorine = new JLabel();
	JLabel phosphorus = new JLabel();
	JLabel sulfur = new JLabel();
	JLabel chlorine = new JLabel();
	JLabel bromine = new JLabel();
	JLabel iodine = new JLabel();
	
	String hydrogenDesc = "<html>Hydrogen<br>Atomic Mass: 1.00794<br>Atomic Radii: 53.0 pm<br>Electronegativity: 2.20";
	String carbonDesc = "<html>Carbon<br>Atomic Mass: 12.0107<br>Atomic Radii: 67.0 pm <br>Electronegativity: 2.55";
	String nitrogenDesc = "<html>Nitrogen<br>Atomic Mass: 14.0067<br>Atomic Radii: 65.0 pm<br>Electronegativity: 3.04";
	String oxygenDesc = "<html>Oxygen<br>Atomic Mass: 15.9994<br>Atomic Radii: 60.0 pm <br>Electronegatvity: 3.44";
	String fluorineDesc = "<html>Fluorine<br>Atomic Mass: 18.9984032<br>Atomic Radii: 50.0 pm<br>Electronegativity: 3.98";
	String phosphorusDesc = "<html>Phosphorus<br>Atomic Mass: <br>Atomic Radii: pm<br>Electronegativity: ";
	String sulfurDesc = "<html>Sulfur<br>Atomic Mass: 32.065<br>Atomic Radii: 100.0 pm<br>Electronegativity: 2.58";
	String chlorineDesc = "<html>Chlorine<br>Atomic Mass: 35.453<br>Atomic Radii: 100.0 pm<br>Electronegativity: 3.16";
	String bromineDesc = "<html>Bromine<br>Atomic Mass: 79.904<br>Atomic Radii: 115.0 pm<br>Electronegativity: 2.96";
	String iodineDesc = "<html>Iodine<br>Atomic Mass: 126.90447<br>Atomic Radii: 140.0 pm<br>Electronegatvity: 2.66";
	
	
	
	AtomChooser()
	{
		//super("Atom Chooser");
		setLayout(new GridLayout(2,5));
		this.setSize(130,75);
		this.setVisible(true);
		this.setResizable(false);
		add(hydrogen);
		add(carbon);
		add(nitrogen);
		add(oxygen);
		add(fluorine);
		add(phosphorus);
		add(sulfur);
		add(chlorine);
		add(bromine);
		add(iodine);
		
	
		hydrogen.setToolTipText(hydrogenDesc);
		carbon.setToolTipText(carbonDesc);
		nitrogen.setToolTipText(nitrogenDesc);
		oxygen.setToolTipText(oxygenDesc);
		fluorine.setToolTipText(fluorineDesc);
		phosphorus.setToolTipText(phosphorusDesc);
		sulfur.setToolTipText(sulfurDesc);
		chlorine.setToolTipText(chlorineDesc);
		bromine.setToolTipText(bromineDesc);
		iodine.setToolTipText(iodineDesc);
		
		hydrogen.addMouseListener(this);
		carbon.addMouseListener(this);
		nitrogen.addMouseListener(this);
		oxygen.addMouseListener(this);
		fluorine.addMouseListener(this);
		phosphorus.addMouseListener(this);
		sulfur.addMouseListener(this);
		chlorine.addMouseListener(this);
		bromine.addMouseListener(this);
		iodine.addMouseListener(this);
		
		this.addWindowListener((WindowListener) this);

	}

	
	public void paint(Graphics g)
	{
		super.paint(g);
		String path = "./AtomChooser1.bmp";
		
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, 130, 140);

		BufferedImage image = null;
		try {
			image = ImageIO.read(new File(path));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if(image != null)
		{
			g.drawImage(image, 0, 10, null);
		}
		else
			System.out.println("Picture Load Error!");
		//super.paint(g);

		
		
	}


	@Override
	public void mouseClicked(MouseEvent e) {
		
		canvas.drawLine = false;
		canvas.drawTextBox = false;
		canvas.drawText = false;
		canvas.drawChain = false;
		canvas.drawBenzene = false;
		canvas.drawCyclohexane = false;
		canvas.drawWedge = false;
		canvas.drawDash = false;
		canvas.drawDouble = false;
		canvas.drawTriple = false;
		canvas.drawIndivAtom = true;
		
		if(e.getSource() == hydrogen)
		{
			System.out.println("You clicked Hydrogen!");
			canvas.indivAtom = "H";
		}
		if(e.getSource() == carbon)
		{
			System.out.println("You clicked Carbon!");
			canvas.indivAtom = "C";
		}
		if(e.getSource() == nitrogen)
		{
			System.out.println("You clicked Nitrogen!");
			canvas.indivAtom = "N";
		}
		if(e.getSource() == oxygen)
		{
			System.out.println("You clicked Oxygen!");
			canvas.indivAtom = "O";
		}
		if(e.getSource() == fluorine)
		{
			System.out.println("You clicked Fluorine!");
			canvas.indivAtom = "F";
		}
		if(e.getSource() == phosphorus)
		{
			System.out.println("You clicked Phosphorus!");
			canvas.indivAtom = "P";
		}
		if(e.getSource() == sulfur)
		{
			System.out.println("You clicked Sulfur!");
			canvas.indivAtom = "S";
		}
		if(e.getSource() == chlorine)
		{	
			System.out.println("You clicked Chlorine!");	
			canvas.indivAtom = "Cl";
		}
		if(e.getSource() == bromine)
		{
			System.out.println("You clicked Bromine!");
			canvas.indivAtom = "Br";
		}
		if(e.getSource() == iodine)
		{
			System.out.println("You clicked Iodine!");
			canvas.indivAtom = "I";
		}
		
		
	}


	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void windowClosed(WindowEvent arg0) {
		
		
	}


	@Override
	public void windowClosing(WindowEvent arg0) {
		chooserInstance = false;
		
	}


	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
}